/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-07-19 15:30:35
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-11-17 10:55:16
 */
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { LazyLoadEvent } from 'primeng/api';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { AffairRemindServiceProxy, CommonServiceProxy, AffairRemindDto, LabelCollectInt } from '@shared/service-proxies/service-proxies';
import { TreeNode } from 'primeng/api';

@Component({
    selector: 'app-message-notice',
    templateUrl: './message-notice.component.html',
    styleUrls: ['./message-notice.component.less']
})
export class MessageNoticeComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    selectType = undefined;
    messageDetail = '';
    display = false;
    files2 = [];
    selectedFiles: any = [];
    selectRow: any = [];
    loadingtree = false;
    messageId = undefined;
    selectTypeOption = [
        {
            label: '全部',
            value: undefined
        }, {
            label: '已读',
            value: 2
        }, {
            label: '未读',
            value: 1
        }
    ]
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _affairRemindServiceProxy: AffairRemindServiceProxy,
    ) {
        super(injector);
    }
    getTreeInfo(): void {
        this.loadingtree = true;
        this._affairRemindServiceProxy.getAffairTree()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                //建立父级节点
                for (const item of result.children) {
                    this.files2.push(item);
                }
                this.expandAll();
                this.loadingtree = false;
            });
    }
    expandAll() {
        this.files2.forEach((node) => {
            node.expandedIcon = "pi pi-folder-open"
            node.collapsedIcon = "pi pi-folder"
            this.expandRecursive(node, true);
        });
    }
    private expandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        node.expandedIcon = "pi pi-folder-open"
        node.collapsedIcon = "pi pi-folder"
        if (node.children) {
            node.children.forEach((childNode) => {
                this.expandRecursive(childNode, isExpand);
            });
        }
    }
    nodeSelect(event) {
        this.selectedFiles = event.node;
        if (event.node.id != null) {
            this.getTableList();
        } else {
            this.primengTableHelper.records = [];
        }
    }
    ngOnInit() {
        this.getTreeInfo();
    }
    onChange(event) {
        this.getTableList();
    }
    delete() {
        this.message.confirm(
            '确认删除这条数据吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    let input = [];
                    for (const item of this.selectRow) {
                        input.push(item.id);
                    }
                    if (this.saveFlag) {
                        this.saveFlag = false;
                        this._affairRemindServiceProxy.deleteBatchAffairMessage(input).subscribe(result => {
                            this.saveFlag = true;
                            if (result.code == 0) {
                                this._messageService.success('操作成功！');
                                this.getTableList();
                            } else {
                                this._messageService.error(result.message);
                            }
                        });
                    }
                }
            }
        );


    }
    markRead() {
        let input = [];
        for (const item of this.selectRow) {
            input.push(item.id);
        }
        if (this.saveFlag) {
            this.saveFlag = false;
            this._affairRemindServiceProxy.updateBatchAffairMessage(input).subscribe(result => {
                this.saveFlag = true;
                if (result.code == 0) {
                    this._messageService.success('操作成功！');
                    this.getTableList();
                } else {
                    this._messageService.error(result.message);
                }
            });
        }

    }
    getTableList(event?: LazyLoadEvent) {
        this._affairRemindServiceProxy.getAffairMessagesByRemindId(
            this.selectedFiles.id,
            this.selectType,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.selectRow = [];
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    selectrow(row) {
        this.display = true;
        this.messageDetail = row.message;
        this.messageId = row.id;
    }
    onHide(event) {
        this._affairRemindServiceProxy.updateAffairMessageById(this.messageId).subscribe(result => {
            this.getTableList();
        });
    }
}
